from data import SourceData
import decimal
from flask import Flask as _Flask, jsonify
from flask import render_template
from flask.json import JSONEncoder as _JSONEncoder
import utils


class JSONEncoder(_JSONEncoder):
    def default(self, o):
        if isinstance(o, decimal.Decimal):
            return float(o)
        super(_JSONEncoder, self).default(o)


class Flask(_Flask):
    json_encoder = JSONEncoder


app = Flask(__name__)

source = SourceData()


@app.route('/')
def index():
    return render_template('index.html')


@app.route('/line')
def line():
    data = source.line
    xAxis = data.pop('legend')
    return render_template('line.html', title='折线图', data=data, legend=list(data.keys()), xAxis=xAxis)


@app.route('/bar')
def bar():
    data = source.bar
    xAxis = data.pop('legend')
    return render_template('bar.html', title='柱形图', data=data, legend=list(data.keys()), xAxis=xAxis)


@app.route('/wordcloud')
def wordcloud():
    data = source.wordcloud
    return render_template('wordcloud.html', title='词云', data=data)


@app.route('/r1')
def get_r1_data():
    data = utils.get_r1_data()
    pts = []
    rebs = []
    ast = []
    for o, p, q in data:
        pts.append(o)
        rebs.append(p)
        ast.append(q)
    return jsonify({"pts": pts, "time": rebs, "att": ast})


@app.route('/l1')
def get_l1_data():
    data = utils.get_l1_data()
    pts, rebs, ast = [], [], []
    for b, c, d in data:
        pts.append(b)
        rebs.append(c)
        ast.append(d)
    return jsonify({"pts": pts, "time": rebs, "ft": ast})


@app.route('/team')
def team():
    return render_template("team.html")


if __name__ == "__main__":
    app.run(host='127.0.0.1', debug=True)
